﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.Entity;
using HRM.Utility;
using HRM.BIZ;

namespace WebUI.UserControls
{
    public delegate void ItemManagementHandler(object sender,ItemManagementEventArgs e);

    public partial class ItemManagement: System.Web.UI.UserControl
    {
        public event ItemManagementHandler Delete;
        public event ItemManagementHandler Add;
        public event ItemManagementHandler SaveEdit;
        private object dataSource = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadItemList();
            }
        }

        protected void grvItem_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkAll = new CheckBox();
                chkAll.ID = "chkAll";
                chkAll.Attributes.Add("onclick", string.Format("selectAll('{0}')", chkAll.ClientID));
                e.Row.Cells[0].Controls.Add(chkAll);
            }
        }

        protected void grvItem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditItem":
                    {
                        btnAction.CommandName = "Edit";
                        string[] args = e.CommandArgument.ToString().Split(';');
                        btnAction.CommandArgument = args[0];
                        txtName.Text = args[1];
                        btnAction.Text = "Lưu";
                        SetActionPanelVisible(true);
                        break;
                    }
                case "DeleteItem":
                    {
                        if (Delete != null)
                        {
                            Delete(this, new ItemManagementEventArgs(e.CommandArgument.ToInt32(), ""));
                        }
                        break;
                    }
            }
        }

        protected void grvItem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (ViewState["datasource"] != null)
                grvItem.DataSource = ViewState["datasource"];
            grvItem.PageIndex = e.NewPageIndex;
            grvItem.DataBind();
        }

        protected void grvItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chk = e.Row.FindControl("chkAll") as CheckBox;
                if (chk != null)
                {
                    chk.Attributes.Add("onclick", string.Format("selectAll('{0}')", chk.ClientID));
                }
            }
        }

        public void LoadItemList()
        {
            grvItem.DataSource = DataSource;
            grvItem.DataBind();
        }

        public object DataSource
        {
            get { return dataSource; }
            set
            {
                ViewState["datasource"] = value;
                dataSource = value;
            }
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            btnAction.CommandArgument = "";
            btnAction.CommandName = "Add";
            btnAction.Text = "Thêm";
            txtName.Text = "";
            SetActionPanelVisible(true);
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            switch (btnAction.CommandName)
            {
                case "Add":
                    {
                        if (Add != null)
                        {
                            Add(this, new ItemManagementEventArgs(-1, txtName.Text));
                        }
                        break;
                    }
                case "Edit":
                    {
                        if (SaveEdit != null)
                        {
                            SaveEdit(this, new ItemManagementEventArgs(btnAction.CommandArgument.ToInt32(), txtName.Text));
                        }
                        break;
                    }
            }
        }

        public void SetActionPanelVisible(bool visible)
        {
            if (visible)
            {
                divAction.Style.Add("display", "block");
            }
            else
            {
                divAction.Style.Add("display", "none");
            }
        }
    }

    public class ItemManagementEventArgs : EventArgs
    {
        public int ID { get; set; }
        public string Name { get; set; }
        
        public ItemManagementEventArgs(int id, string name)
        {
            ID = id;
            Name = name;
        }
    }
}